package com.itcncp.ucenter.web.controller;

import com.itcncp.common.base.BaseController;
import com.itcncp.ucenter.common.ApiResult;
import com.itcncp.ucenter.common.ApiResultConstant;
import com.itcncp.ucenter.dao.model.UserRechangeOpJnl;
import com.itcncp.ucenter.dao.model.UserRechangeOpJnlExample;
import com.itcncp.ucenter.rpc.api.UserAccountInfoService;
import com.itcncp.ucenter.rpc.api.UserCustomerInfoService;
import com.itcncp.ucenter.rpc.api.UserRechangeOpJnlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 *
 * Created by shuitcncp on 2016/12/10.
 */
@Controller
@RequestMapping("/recharge")
@Api(tags = "recharge",value = "用户充值", description = "充值的基本信息接口",
        produces = "produces",consumes = "consumes",basePath = "/recharge")

public class UserRechargeController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserRechargeController.class);

    @Autowired
    private UserCustomerInfoService userCustomerInfoService;
    @Autowired
    private UserAccountInfoService userAccountInfoService;
    @Autowired
    private UserRechangeOpJnlService userRechangeOpJnlService;


    /**
     * pingpp创建充值订单
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/rechargeAmount", method = RequestMethod.POST)
    public  Object rechargeAmount(HttpServletRequest request,
                                    @RequestParam(required = true,defaultValue = "",value = "userId") Long userId,
                                    @RequestParam(required = true,defaultValue = "",value = "amount") BigDecimal amount,
                                    @RequestParam(required = true,defaultValue = "",value = "channel") String channel) {


        //用戶id不能爲空
        if(userId<=0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL);
        }

        //充值金額不得小於0
        if(amount.doubleValue()<=0){
            return new ApiResult( ApiResultConstant.FAILED);
        }

        //充值渠道不能爲空
        if(StringUtils.isEmpty( channel )){
            return new ApiResult( ApiResultConstant.FAILED);
        }

        Object charge = userRechangeOpJnlService.pingppRechargeAmount(userId, amount,channel);

        if(null != charge){
            return  new ApiResult( ApiResultConstant.SUCCESS,charge);
        }

        return new ApiResult( ApiResultConstant.FAILED,charge);
    }


    /**
     * 充值记录
     * @return
     */
    @ResponseBody
    @ApiResponse(code=200,message = "充值记录",response =UserRechangeOpJnl.class)
    @RequestMapping(value = "/findRechargeList", method = RequestMethod.POST)
    public Object findRechargeList(HttpServletRequest request,
                               @RequestParam(required = false, defaultValue = "0", value = "pageSize") int pageSize,
                               @RequestParam(required = false, defaultValue = "5", value = "pageIndex") int pageIndex,
                               @RequestParam(required = false, defaultValue = "", value = "userId") Long userId){


        if(userId <= 0){
            return new ApiResult( ApiResultConstant.APP_ILLEGAL );
        }

        UserRechangeOpJnlExample userRechangeOpHisExample = new UserRechangeOpJnlExample();
        userRechangeOpHisExample.createCriteria().andUserIdEqualTo( userId );

        //排序
        userRechangeOpHisExample.setOrderByClause("recharge_no desc");
        //分页查询
        List<UserRechangeOpJnl> userRechangeOpHis = userRechangeOpJnlService
                .selectByExampleForStartPage(userRechangeOpHisExample, pageSize, pageIndex);

        return new ApiResult( ApiResultConstant.SUCCESS,userRechangeOpHis);
    }

    /**
     * 根据订单号获取充值详情
     * @return
     */
    @ResponseBody
    @ApiResponse(code=200,message = "充值详情",response =UserRechangeOpJnl.class)
    @RequestMapping(value = "/getRechargeOrderNo", method = RequestMethod.POST)
    public Object getRechargeOrderNo(HttpServletRequest request,
                                   @RequestParam(required = false, defaultValue = "0", value = "orderNo") Long orderNo){


        UserRechangeOpJnl userRechangeOpHis = userRechangeOpJnlService.selectByPrimaryKey( orderNo  );

        if(null!=userRechangeOpHis){
            return  new ApiResult( ApiResultConstant.SUCCESS,userRechangeOpHis);
        }

        return new ApiResult( ApiResultConstant.FAILED);
    }



}